//
//  FindEventView.m
//  eticket
//
//  Created by AnhQuocNguyen on 4/12/13.
//  Copyright (c) 2013 SoinMedia. All rights reserved.
//

#import "FindEventView.h"

@implementation FindEventView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
    }
    return self;
}


//////////////////////////////////////////////////////////////////////////////////////////
#pragma mark - setupUI
- (void)setupUI{
    
    btnCategory.exclusiveTouch = YES;
    btnCountry.exclusiveTouch = YES;
    btnSearch.exclusiveTouch = YES;
    txtEventName.delegate = self;
    txtEventName.clearButtonMode = UITextFieldViewModeWhileEditing;
    
    _finishCountries = NO;
    _finishCategory = NO;
    _countTimeout = 0;
    _isClick = NO;
    
    mLoading = YES;
    [self initHUD:@"Loading...."];
    [mHud showWhileExecuting:@selector(waitting) onTarget:self withObject:nil animated:NO];
    
    
    
    //Load xong country roi moi load category
    dropdown_Countries = [[DropdownboxView alloc] initWithFrame:CGRectMake(13, 141, 300, 0)];
    dropdown_Countries.typeAPI = countries_API;
    dropdown_Countries.dropdownboxViewDelegate = self;
    [dropdown_Countries getDataFromServer];
    dropdown_Countries.exclusiveTouch = YES;
    [self addSubview:dropdown_Countries];
    
    
    }
- (void)enableUI:(BOOL)enable{
    txtEventName.userInteractionEnabled = enable;
    btnCountry.userInteractionEnabled = enable;
    btnSearch.userInteractionEnabled = enable;
    btnCategory.userInteractionEnabled = enable;
}
- (void)downKeyboar{
    [txtEventName resignFirstResponder];
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
    SAFE_RELEASE_OBJECT(_strEventName);
    _strEventName = [[Utilities trimString:txtEventName.text] retain];
}


//////////////////////////////////////////////////////////////////////////////////////////
#pragma mark - DropdownboxViewDelegate
- (void)DropdownboxView:(DropdownboxView *)dropdownboxView didSelectedCountry:(Country *)country{
    [UIView animateWithDuration:0.2 animations:^{
        
        if ([dropdownboxView.typeAPI isEqualToString:countries_API]) {
            CGRect frame_dropdown_Countries = dropdown_Countries.frame;
            frame_dropdown_Countries.size.height = 0;
            dropdown_Countries.frame = frame_dropdown_Countries;
            
            UITableView *tableCountries = (UITableView *)[dropdown_Countries.subviews objectAtIndex:0];
            CGRect frame_tableCountries = tableCountries.frame;
            frame_tableCountries.size.height = frame_dropdown_Countries.size.height;
            tableCountries.frame = frame_tableCountries;

        }else{
            
            CGRect frame_dropdown_Category = dropdown_Category.frame;
            frame_dropdown_Category.size.height = 0;
            dropdown_Category.frame = frame_dropdown_Category;
            
            UITableView *tableCategory = (UITableView *)[dropdown_Category.subviews objectAtIndex:0];
            CGRect frame_tableCountries = tableCategory.frame;
            frame_tableCountries.size.height = frame_dropdown_Category.size.height;
            tableCategory.frame = frame_tableCountries;

        }
    } completion:^(BOOL finished) {
        _isClick = !_isClick;
        if (!_isClick) {
            txtEventName.userInteractionEnabled = YES;
            btnCountry.userInteractionEnabled = YES;
            btnSearch.userInteractionEnabled = YES;
            btnCategory.userInteractionEnabled = YES;
        }
    
        _lbCountry.text = country.title;
        SAFE_RELEASE_OBJECT(_strConutry);
        _strConutry = country.idCountry;
    }];
}
- (void)DropdownboxView:(DropdownboxView *)dropdownboxView didSelectedCategory:(CategoryEvent *)category{
    
    [UIView animateWithDuration:0.2 animations:^{
        
        if ([dropdownboxView.typeAPI isEqualToString:countries_API]) {
            CGRect frame_dropdown_Countries = dropdown_Countries.frame;
            frame_dropdown_Countries.size.height = 0;
            dropdown_Countries.frame = frame_dropdown_Countries;
            
            UITableView *tableCountries = (UITableView *)[dropdown_Countries.subviews objectAtIndex:0];
            CGRect frame_tableCountries = tableCountries.frame;
            frame_tableCountries.size.height = frame_dropdown_Countries.size.height;
            tableCountries.frame = frame_tableCountries;
            
        }else{
            
            CGRect frame_dropdown_Category = dropdown_Category.frame;
            frame_dropdown_Category.size.height = 0;
            dropdown_Category.frame = frame_dropdown_Category;
            
            UITableView *tableCategory = (UITableView *)[dropdown_Category.subviews objectAtIndex:0];
            CGRect frame_tableCountries = tableCategory.frame;
            frame_tableCountries.size.height = frame_dropdown_Category.size.height;
            tableCategory.frame = frame_tableCountries;
            
        }
    } completion:^(BOOL finished) {
        _isClick = !_isClick;
        if (!_isClick) {
            txtEventName.userInteractionEnabled = YES;
            btnCountry.userInteractionEnabled = YES;
            btnSearch.userInteractionEnabled = YES;
            btnCategory.userInteractionEnabled = YES;
        }
        
        _lbCategory.text = category.title;
        SAFE_RELEASE_OBJECT(_strCategory);
        _strCategory = [category.idCategory retain];
    }];
}
- (void)DropdownboxView:(DropdownboxView *)dropdownboxView didFinishDownload:(NSInteger)numberData withTypeDropboxIs:(NSString *)typeDropbox{
    if ([typeDropbox isEqualToString:countries_API]) {
        _finishCountries = YES;
        NSLog(@"da load xong contries vao combobox");
        
        //Load category
        dropdown_Category = [[DropdownboxView alloc] initWithFrame:CGRectMake(13, 181, 300, 0)];
        dropdown_Category.exclusiveTouch = YES;
        dropdown_Category.typeAPI = categories_API;
        dropdown_Category.dropdownboxViewDelegate = self;
        [dropdown_Category getDataFromServer];
        [self addSubview:dropdown_Category];
    }
    else{
         mLoading = NO;
        _finishCategory = YES;
        NSLog(@"da load xong category vao combobox");
    }
}
- (void)DropdownboxViewLoadingDataFail:(DropdownboxView *)dropdownboxView{
    mLoading = NO;
}
- (void)DropdownboxViewRefeshData:(DropdownboxView *)dropdownboxView{
    mLoading = YES;
    [self initHUD:@"Loading...."];
    [mHud showWhileExecuting:@selector(waitting) onTarget:self withObject:nil animated:NO];
}
- (void)DropdownboxViewCancelRefeshData:(DropdownboxView *)dropdownboxView{
    if ([_findEventViewDelegate respondsToSelector:@selector(findEventViewLoadingFail:)]) {
        [_findEventViewDelegate findEventViewLoadingFail:self];
    }
}



//////////////////////////////////////////////////////////////////////////////////////////
#pragma mark - btn_Clicked
- (IBAction)btn_Clicked:(id)sender{
    if (!_finishCategory || !_finishCountries) {
        NSLog(@"Chua load xong data");
        return;
    }
    
    ////////////////////////////////////////////////////
    UIButton *button = (UIButton *)sender;
    if (button == btnCountry) {
        txtEventName.userInteractionEnabled = NO;
        btnCategory.userInteractionEnabled = NO;
        btnSearch.userInteractionEnabled = NO;
        ////////////////////////////////////////////////////
        [UIView animateWithDuration:0.2 animations:^{
           
            CGRect frame_dropdown_Countries = dropdown_Countries.frame;
            frame_dropdown_Countries.size.height = !_isClick ? 210 : 0;
            dropdown_Countries.frame = frame_dropdown_Countries;
           
            UITableView *tableCountries = (UITableView *)[dropdown_Countries.subviews objectAtIndex:0];
            CGRect frame_tableCountries = tableCountries.frame;
            frame_tableCountries.size.height = frame_dropdown_Countries.size.height;
            tableCountries.frame = frame_tableCountries;
            
        } completion:^(BOOL finished) {
            _isClick = !_isClick;
            if (!_isClick) {
                txtEventName.userInteractionEnabled = YES;
                btnCategory.userInteractionEnabled = YES;
                btnSearch.userInteractionEnabled = YES;
            }
        }];
    }
    
    
    if (button == btnCategory) {
        
        
        ////////////////////////////////////////////////////
        [UIView animateWithDuration:0.2 animations:^{
            txtEventName.userInteractionEnabled = NO;
            btnCountry.userInteractionEnabled = NO;
            btnSearch.userInteractionEnabled = NO;
            CGRect frame_dropdown_Category = dropdown_Category.frame;
            frame_dropdown_Category.size.height = !_isClick ? 210 : 0;
            dropdown_Category.frame = frame_dropdown_Category;
            
            UITableView *tableCategory = (UITableView *)[dropdown_Category.subviews objectAtIndex:0];
            CGRect frame_tableCountries = tableCategory.frame;
            frame_tableCountries.size.height = frame_dropdown_Category.size.height;
            tableCategory.frame = frame_tableCountries;
            
        } completion:^(BOOL finished) {
            _isClick = !_isClick;
            if (!_isClick) {
                txtEventName.userInteractionEnabled = YES;
                btnCountry.userInteractionEnabled = YES;
                btnSearch.userInteractionEnabled = YES;
            }
        }];
    }
    
}


#pragma mark - btn_Search
- (IBAction)btn_Search:(id)sender{
    NSString *strTextBox = [Utilities trimString:txtEventName.text];

    if (![Utilities isEmptyString:strTextBox] && [strTextBox length] <= 2) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"event name must be more than 2 character!!!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];
        SAFE_RELEASE_OBJECT(alert);
        return;
    }
    
//    NSLog(@"event name: %@ \n country: %@ \n category: %@", _strEventName, _strConutry, _strCategory);
   
    __block NSMutableString *strSearch = [NSMutableString string];
    [strSearch appendString:@""];
    if (_strEventName && [_strEventName length] > 2) {
        [strSearch appendString:[NSString stringWithFormat:@"&title=%@", _strEventName]];
    }
    if (_strCategory && [_strCategory length] > 0 && ![_strCategory isEqualToString:@"-100"]) {
        [strSearch appendString:[NSString stringWithFormat:@"&category_id=%@", _strCategory]];
    }
    if (_strConutry && [_strConutry length] > 0 && ![_strConutry isEqualToString:@"-100"]) {
        [strSearch appendString:[NSString stringWithFormat:@"&country_id=%@", _strConutry]];
    }
    
    if ([strSearch isEqualToString:@""]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"Input search !!!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];
        SAFE_RELEASE_OBJECT(alert);
        return;
    }
    
   NSString *urlString = [NSString stringWithFormat:@"%@%@%@%@%@", URL_island_API,events_API,search_API, TOKEN__island, strSearch];
    if (!_finalFindEvent) {
        POOL_START
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"FinalFindEventView" owner:self options:nil];
        _finalFindEvent = (FinalFindEventView *)[nib objectAtIndex:0];
       
        _finalFindEvent.frame = CGRectMake(330, [Utilities isDeviceFourInches] ? 38 : 43, 320, [UIScreen mainScreen].bounds.size.height - 43);
        [self.superview addSubview:_finalFindEvent];
        POOL_END
    }
    
    [UIView animateWithDuration:0.3 animations:^{
        
        CGRect frameTempCategory = self.frame;
        frameTempCategory.origin.x = - 320;
        self.frame = frameTempCategory;
        
        CGRect frametemp = _finalFindEvent.frame;
        frametemp.origin.x = 0;
        _finalFindEvent.frame = frametemp;
        
    } completion:^(BOOL finished) {
        self.hidden = YES;
        _finalFindEvent.urlString = urlString;
        if ([_lbCategory.text isEqualToString:@"NONE"] || [_lbCategory.text isEqualToString:@"Category"]) {
            _finalFindEvent.category = @"...";
        }
        else{
            _finalFindEvent.category = _lbCategory.text;
        }
         _finalFindEvent.finalFindEventDelegate = self;
        [_finalFindEvent reloadData];
        if ([_findEventViewDelegate respondsToSelector:@selector(findEventView:tapButtonSearch:)]) {
            [_findEventViewDelegate findEventView:self tapButtonSearch:_finalFindEvent];
        }
    }];

}

#pragma mark - btnFindEvent_MyTicket_CLicked
- (IBAction)btnFindEvent_MyTicket_CLicked:(id)sender{
    if ([_findEventViewDelegate respondsToSelector:@selector(findEventViewClickMyTicket:)]) {
        [_findEventViewDelegate findEventViewClickMyTicket:self];
    }
}



//////////////////////////////////////////////////////////////////////////////////////////
#pragma mark - finalFindEvent Delegate
- (void)finalFindEventWithBack:(FinalFindEventView *)finalFindEvent{
    self.hidden = NO;
    [UIView animateWithDuration:0.3 animations:^{
        
        CGRect frameTempCategory = self.frame;
        frameTempCategory.origin.x = 0;
        self.frame = frameTempCategory;
        
        CGRect frametemp = _finalFindEvent.frame;
        frametemp.origin.x = 330;
        _finalFindEvent.frame = frametemp;
        
    } completion:^(BOOL finished) {
        if ([_findEventViewDelegate respondsToSelector:@selector(findEventView:backFromFinalFindEventView:)]) {
            [_findEventViewDelegate findEventView:self backFromFinalFindEventView:_finalFindEvent];
        }
    }];
    
}



#pragma mark - UITextField Delegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}

#pragma mark - MBProgressHUD
-(void)initHUD:(NSString *)alert {
    if (mHud) {
        [mHud removeFromSuperview];
        SAFE_RELEASE_OBJECT(mHud);
    }
    mHud = [[MBProgressHUD alloc] initWithView:self];
    mHud.labelText = alert;
    mHud.dimBackground = YES;
    [self addSubview:mHud];
    
}
-(void)waitting{
    while (mLoading) {
        usleep(5000);
    }
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 1) {
        if (!_finishCountries) {
            [dropdown_Countries getDataFromServer];
        }
        if (!_finishCategory) {
            [dropdown_Category getDataFromServer];
        }
    }
}

- (void)dealloc {
    [btnCountry release];
    [btnCategory release];
    [btnSearch release];
    [txtEventName release];
    [_lbCountry release];
    [_lbCategory release];
    SAFE_RELEASE_VIEW(mHud);
    
    SAFE_RELEASE_OBJECT(_finalFindEvent);
    SAFE_RELEASE_OBJECT(_strEventName);
    SAFE_RELEASE_OBJECT(_strConutry);
    SAFE_RELEASE_OBJECT(_strCategory);
    [super dealloc];
}
@end
